package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {
    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealDishService setmealDishService;

    @Override
    public void saveWithFlavor(DishDto dishDto) {
        //保存菜品信息
        save(dishDto);
        //获取一个菜品的多个口味信息,保存
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(dishFlavor -> {
            //口味与菜品绑定
            dishFlavor.setDishId(dishDto.getId());
        });
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    @Transactional
    public DishDto getByIdWithFlavor(Long id) {
        //从dish表查询菜品基本信息
        Dish dish = getById(id);
        //查询菜品对应口味信息
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, id);
        List<DishFlavor> flavors = dishFlavorService.list(lqw);
        //复制菜品信息到dto
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    @Override
    public void update(DishDto dishDto) {
        //获取当前菜品信息,更新菜品信息
        updateById(dishDto);
        //同时更新对应口味:删除口味信息,重新添加
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId, dishDto.getId());
        //删除当前菜品的口味
        dishFlavorService.remove(lqw);

        //重新添加flavor
        List<DishFlavor> flavors = dishDto.getFlavors();
        //将每一条flavor添加到当前菜品
        flavors.forEach(
                flavor -> {
                    flavor.setDishId(dishDto.getId());
                }
        );
       /* flavors=flavors.stream().map((flavor)->{
                flavor.setDishId(dishDto.getId());
                return flavor;
        }).collect(Collectors.toList());*/
        //保存修改
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    public void removeByCondition(List<Long> ids) {
        //删除dish,判断是否为停售状态;
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.in(Dish::getId, ids).eq(Dish::getStatus, 1);
        if (count(lqw) > 0) {
            //存在启售的dish,不能删除
            throw new CustomException("菜品正在启售,不能删除");
        }
        // 全部停售,判断套餐setmeal中不存在对应
        // select * from setmeal_dish where dish_id in (,,,)
        LambdaQueryWrapper<SetmealDish> lqw2 = new LambdaQueryWrapper<>();
        lqw2.in(SetmealDish::getDishId, ids);
        int count2 = setmealDishService.count(lqw2);
        if (count2>0){
            //setmeal中存在dish,不能删除
            throw new CustomException("有套餐正在售卖此菜品");
        }
        // 删除菜品,同时将口味表中对应的数据删除;
        removeByIds(ids);
        LambdaQueryWrapper<DishFlavor> lqw3=new LambdaQueryWrapper<>();
        //delete * FROM `dish_flavor` WHERE dishid IN ()
        lqw3.in(DishFlavor::getDishId,ids);
        dishFlavorService.remove(lqw3);
    }
}
